<template>
  <div v-loading="loading" class="abort-data">
    <el-dialog v-loading="loading" element-loading-background="rgba(0, 0, 0, 0.2)" :visible.sync="dialogShow" title="终止数据" :close-on-click-modal="false" @close="handleBack">
      <el-form ref="form" :model="formData" :rules="formRules" label-width="80px">
        <el-form-item prop="endReason" label="终止原因">
          <el-input v-model="formData.endReason" type="textarea" :autosize="{minRows:3}" placeholder="终止原因" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button size="mini" @click="handleBack">取消</el-button>
        <el-button type="primary" size="mini" @click="confirmHandle">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { stopData } from '@/api/proConfig'
export default {
  name: 'Abort',
  props: {
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    detailData: {
      type: [String, Object],
      default: ''
    }
  },
  data() {
    return {
      loading: false,
      formData: {
        reason: ''
      },
      formRules: {
        endReason: [{ required: true, message: '请输入终止原因' }]
      },
      tableData: []
    }
  },
  computed: {
    dialogShow: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  created() {
    this.tableData = this.detailData
  },
  methods: {
    // 返回
    handleBack() {
      this.dialogShow = false
      this.clearForm()
      this.$emit('success')
    },
    clearForm(done) {
      this.formData = {
        constData: [],
        staff: []
      }
      this.$refs.rulesForm && this.$refs.rulesForm.clearValidate()
      done && done()
    },
    confirmHandle() {
      this.$refs.form.validate(validate => {
        if (validate) {
          this.loading = true
          const obj = {}
          const ids = this.tableData.map(item => item.id)
          obj.endReason = this.formData.endReason
          obj.ids = ids
          obj.proConfigType = 'SANITARY'
          stopData(obj).then(res => {
            if (res.success) {
              this.$message.success('操作成功')
              this.dialogShow = false
              this.clearForm()
              this.$emit('success', false)
            }
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.abort-data{
  display: inline-block;
  margin: 0 10px;
}
</style>
